package com.muyu.system.controller;

import com.muyu.common.core.constant.CacheConstants;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.redis.service.RedisService;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.system.domain.SysLogininfor;
import com.muyu.system.domain.model.SysLogininforAddModel;
import com.muyu.system.domain.model.SysLogininforExportModel;
import com.muyu.system.domain.model.SysLogininforListModel;
import com.muyu.system.domain.req.SysLogininforAddReq;
import com.muyu.system.domain.req.SysLogininforListReq;
import com.muyu.system.domain.resp.SysLogininforListResp;
import com.muyu.system.service.SysLogininforService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统访问记录
 *
 * @author muyu
 */
@RestController
@RequestMapping("/logininfor")
public class SysLogininforController extends BaseController {
    private final SysLogininforService logininforService;

    private final RedisService redisService;

    @Autowired
    public SysLogininforController(SysLogininforService logininforService, RedisService redisService) {
        this.logininforService = logininforService;
        this.redisService = redisService;
    }

    @RequiresPermissions("system:logininfor:list")
    @PostMapping("/list")
    public Result<DataPageResp<SysLogininforListResp>> list (@RequestBody SysLogininforListReq sysLogininforListReq) {
        PageQueryModel<SysLogininforListModel> pageQueryModel = logininforService.pageQuery(SysLogininforListModel.reqBuild(sysLogininforListReq));
        DataPageResp<SysLogininforListModel> sysLogininforListModelDataPageResp = DataPageResp.of(pageQueryModel);
        return Result.success(
                DataPageResp.<SysLogininforListResp>builder()
                .rows(sysLogininforListModelDataPageResp.getRows()
                        .stream()
                        .map(SysLogininforListResp::buildSysLogininforResp)
                        .toList())
                .total(sysLogininforListModelDataPageResp.getTotal())
                .build()
        );
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:logininfor:export")
    @PostMapping("/export")
    public void export (HttpServletResponse response, SysLogininforListReq sysLogininforListReq) {
        List<SysLogininfor> sysLogininforList = logininforService.exportQuery(SysLogininforExportModel.reqBuild(sysLogininforListReq));
        ExcelUtil<SysLogininfor> util = new ExcelUtil<>(SysLogininfor.class);
        util.exportExcel(response, sysLogininforList, "登录日志");
    }

    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public Result<String> remove (@PathVariable("infoIds") List<Long> infoIds) {
        logininforService.deleteLogininforByIds(infoIds);
        return Result.success();
    }

    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public Result<String> clean () {
        logininforService.cleanLogininfor();
        return Result.success();
    }

    @RequiresPermissions("system:logininfor:unlock")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public Result<String> unlock (@PathVariable("userName") String userName) {
        redisService.deleteObject(CacheConstants.PWD_ERR_CNT_KEY + userName);
        return Result.success();
    }

    /*@InnerAuth*/
    @PostMapping
    public Result<String> add (@RequestBody SysLogininforAddReq logininforAddReq) {
        logininforService.insertLogininfor(SysLogininforAddModel.of(logininforAddReq));
        return Result.success();
    }
}
